package com.tnxst.phexamination.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.tnxst.phexamination.R;
import com.tnxst.phexamination.adapter.ServiceDetailsAdapter;
import com.tnxst.phexamination.constant.HISConfig;
import com.tnxst.phexamination.databinding.ServicePackageDetailsBinding;
import com.tnxst.phexamination.dto.ServiceDetailsDTO;
import com.tnxst.phexamination.dto.ServicePackageDTO;
import com.tnxst.phexamination.public_health_docking_interface.FamilySignInterface;
import com.tnxst.phexamination.utils.ReflectUtils;
import com.tnxst.phexamination.utils.StringUtils;
import com.tnxst.phexamination.utils.Utils;
import com.tnxst.phexamination.utils.WebServiceUtils;
import com.tnxst.phexamination.viewmodel.GlobalViewModel;
import com.tnxst.phexamination.vo.ServicesVo;

import org.angmarch.views.NiceSpinner;
import org.angmarch.views.OnSpinnerItemSelectedListener;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class PackageServiceFragment extends Fragment {

    private GlobalViewModel viewModel;

    private ServicePackageDTO dto;

    private ServicePackageDetailsBinding binding;

    private List<String> level;

    private List<String> isFree;

    private List<ServiceDetailsDTO> serviceDetailDTOS;

    private ServiceDetailsAdapter serviceDetailsAdapter;


    private PackageServiceFragment(){

    }

    public static PackageServiceFragment newInstance(){
        return new PackageServiceFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.service_package_details, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel = ViewModelProviders.of(requireActivity()).get(GlobalViewModel.class);
        initData();
        initView();
    }

    private void initView() {

        if (dto.getAPPOBJECT() != null)
            binding.editText55.setText(dto.getAPPOBJECT());

        if (dto.getSERVICENAME() != null)
            binding.editText58.setText(dto.getSERVICENAME());

        if (dto.getFEE() != null)
            binding.editText59.setText(dto.getFEE());

        if (dto.getREMARK() != null)
            binding.info.setText(dto.getREMARK());

        binding.button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideAddFragment();
            }
        });

        binding.button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.getServicePackageRepository().insert(dto);
//                if (StringUtils.isNotNull(HISConfig.PRODUCT_CODE)){
//                    ServicesVo servicesVo = new ServicesVo();
//                    servicesVo.setAppObject(dto.getAPPOBJECT());
//                    servicesVo.setCode(dto.getCODE());
//                    servicesVo.setFee(dto.getFEE());
//                    servicesVo.setID(dto.getSERVICEID());
//                    servicesVo.setName(dto.getSERVICENAME());
//                    servicesVo.setRemark(dto.getREMARK());
//                    FamilySignInterface.saveOrUpdateOrgPack(servicesVo, new WebServiceUtils.Response() {
//                        @Override
//                        public void onSuccess(Object result) {
//                            Utils.Toast(getContext(),result.toString());
//                        }
//
//                        @Override
//                        public void onError(String error) {
//                            Utils.Toast(getContext(),"上传保存失败！" + error);
//                        }
//                    });
//                }
                hideAddFragment();
            }
        });

        binding.niceSpinner.attachDataSource(level);
        if ( "1".equals(dto.getLEV()))
            binding.niceSpinner.setSelectedIndex(0);
        else if ( "2".equals(dto.getLEV()))
            binding.niceSpinner.setSelectedIndex(1);
        binding.niceSpinner.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
            @Override
            public void onItemSelected(NiceSpinner parent, View view, int position, long id) {
                position = position + 1;
                dto.setLEV(String.valueOf(position));
            }
        });

        binding.niceSpinner2.attachDataSource(isFree);
        if ("0".equals(dto.getISFREE()))
            binding.niceSpinner2.setSelectedIndex(0);
        if ("1".equals(dto.getISFREE()))
            binding.niceSpinner2.setSelectedIndex(1);
        binding.niceSpinner2.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
            @Override
            public void onItemSelected(NiceSpinner parent, View view, int position, long id) {
                dto.setISFREE(String.valueOf(position));
            }
        });

        serviceDetailsAdapter = new ServiceDetailsAdapter(serviceDetailDTOS);
        binding.recycler.setAdapter(serviceDetailsAdapter);
        binding.recycler.setLayoutManager(new LinearLayoutManager(getContext()));



    }

    private void initData() {
        Bundle arguments = getArguments();
        dto = (ServicePackageDTO) arguments.get("servicePackage");
        if (dto == null){
            Utils.Toast(getActivity(),"数据传递失败！");
            hideAddFragment();
        }

        level = new LinkedList<>();
        level.add("县级");
        level.add("机构级");

        isFree = new LinkedList<>();
        isFree.add("收费");
        isFree.add("免费");

        serviceDetailDTOS = viewModel.getServiceDetailsRepository().findAll();

//        if (StringUtils.isNotNull(HISConfig.PRODUCT_CODE)){
//            FamilySignInterface.getPackContentByPackId(dto.getSERVICEID(), -1, -1, new WebServiceUtils.Response<List>() {
//                @Override
//                public void onSuccess(List result) {
//                    serviceDetailDTOS.clear();
//                    for (Object o : result) {
//                        Map data = (Map) o;
//                        ServiceDetailsDTO serviceDetailsDTO = (ServiceDetailsDTO) ReflectUtils.mapToObject(data, ServiceDetailsDTO.class);
//                        serviceDetailDTOS.add(serviceDetailsDTO);
//                    }
//                    serviceDetailsAdapter.setSignPackages(serviceDetailDTOS);
//                    serviceDetailsAdapter.notifyDataSetChanged();
//                    viewModel.getServiceDetailsRepository().insert(serviceDetailDTOS.toArray(new ServiceDetailsDTO[serviceDetailDTOS.size()]));
//                }
//
//                @Override
//                public void onError(String error) {
//                    Utils.Toast(getActivity(),error);
//                }
//            });
//        }

    }

    private void hideAddFragment() {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.setCustomAnimations(0, R.anim.fragment_slide_down);
        ft.hide(this);
        ft.commitAllowingStateLoss();
    }
}
